/****************************************************
    Author:            龙之介
    CreatTime:    2020/12/26 1:34:35
    Description:     Nothing
*****************************************************/

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.UI;
using ZYKTool;
using System.Linq;
using ZYKTool.Timer;

namespace LongZhiJie
{
	public class BossBullet :MonoBehaviour
	{
        [Header("子弹本身参数")]
        [Space]

        public float lifeTime;
        public float distance;
        public LayerMask whatIsSolid;
        public bool Is有毒的 = false;
        public GameObject destroyEffect;
        public bool Is毒块 = false;
        public bool Is禁锢 = false;
        private bool IsColliderWall = false;
        [Header("龙之介Boss框架参数")]
        [Space]
        public float Damage;
        public float speed;
        public Vector3 dir;
        
        private void Start()
        {
            //Debug.LogError(lifeTime);
            if (Is毒块) return;
            Invoke("DestroyProjectile", lifeTime);
 
        }
        private int _禁锢ID = -1;
        private void Update()
        {
            RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, transform.up, distance, whatIsSolid);
            if (hitInfo.collider != null)
            {
                if (hitInfo.collider.CompareTag("Player"))
                {
                    if (TapTapStaticValue.PlayerAttributes.Is中毒)
                    {
                        TapTapStaticValue.TapTapModelManger.Damage(BossName.None, Damage+1);
                    }
                   
                    if (Is有毒的)//中毒标记
                    {

                        TapTapStaticValue.PlayerAttributes.Is中毒 = true;
                        TapTapStaticValue.Player.transform.GetOrAddComponent<SpriteRenderer>().color = Color.green;
                        TapTapStaticValue.Player.GetOrAddComponent<TapTapPlayerLogic>().Player解除中毒();
                    }
                    if(Is禁锢)
                    {
                        TapTapStaticValue.PlayerAttributes.Is禁锢 = true;
                        if (_禁锢ID==-1)
                        {
                            _禁锢ID=ZYKTool.Timer.ZYKTimerSystemTool.Instance.ZYKTimerAddTimerTask(
                                () => { TapTapStaticValue.PlayerAttributes.Is禁锢 = false; }, 1, 1);
                        }
                        else
                        {
                             ZYKTool.Timer.ZYKTimerSystemTool.Instance.ZYKTimerReplaceTimeTask(_禁锢ID,
                                 () => { TapTapStaticValue.PlayerAttributes.Is禁锢 = false; }, 1, 1);
                        }
           
                    }
                    TapTapStaticValue.TapTapModelManger.Damage(BossName.None,Damage );
                    DestroyProjectile();
                }
                if (Is毒块 && IsColliderWall) return;//毒块落地
                if (hitInfo.collider.CompareTag("Up"))
                {
                    毒块落地();

                }
                if (hitInfo.collider.CompareTag("Down"))
                {
                    毒块落地();
                }
                if (hitInfo.collider.CompareTag("Right"))
                {
                    毒块落地();
                }
                if (hitInfo.collider.CompareTag("Left"))
                {
                    毒块落地();
                }
            }
            if (Is毒块 && IsColliderWall) return;//毒块落地
            transform.Translate(dir * speed * Time.deltaTime);
        }
        private void 毒块落地()
        {
            IsColliderWall = true;
            Destroy(gameObject, 1);
        }
        void DestroyProjectile()
        {
            Destroy(Instantiate(destroyEffect, transform.position, Quaternion.identity), 1);
            Destroy(gameObject);
        }
    }
}